package com.ee.chat.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.admin.service.IChannelConfigService;
import com.ee.chat.admin.service.IOperationService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.ChannelConfigMapper;
import com.ee.chat.common.entity.ChannelConfig;
import com.ee.chat.common.service.IChannelShortUrlService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Shane
 * @Date 2020/4/17 14:57
 */
@Service
public class ChannelConfigServiceImpl implements IChannelConfigService {
    @Resource
    ChannelConfigMapper channelConfigMapper;
    @Resource
    IOperationService operationService;
    @Resource
    IChannelShortUrlService channelShortUrlService;

    @Override
    public Result list(HttpServletRequest request, Integer page, Integer limit, Integer status) throws Exception {
        Map<String, Object> data = new HashMap<>();

        if(status == null){
            HttpSession session = request.getSession(true);
            List<String> userPermissions = (List<String>)session.getAttribute("userPermissions");
            if(!userPermissions.contains("/channelConfig/viewList")){
                data.put("datas", new ArrayList<>());
                data.put("total", 0);

                return Result.returnSuccess(data);
            }
        }

        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<Map<String, Object>> iPage = channelConfigMapper.list(p, status);

        data.put("datas", iPage.getRecords());
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "查看落地页列表数据");

        return Result.returnSuccess(data);
    }

    @Override
    public Result toEdit(HttpServletRequest request, Integer id) throws Exception {
        Map<String, Object> data = new HashMap<>();

        ChannelConfig channelConfig = channelConfigMapper.selectById(id);
        data.put("channelConfig", channelConfig == null ? new ChannelConfig() : channelConfig);

        return Result.returnSuccess(data);
    }

    @Override
    public Result save(HttpServletRequest request, ChannelConfig channelConfig) throws Exception {
        if(channelConfig.getId() == null){
            channelConfigMapper.insert(channelConfig);
            operationService.addOperationLog(request, "添加新的落地页，id："+channelConfig.getId());

            //添加落地页对应渠道的短网址
            channelShortUrlService.addChannelShortUrlByCid(channelConfig.getId());
        }else{
            ChannelConfig config = channelConfigMapper.selectById(channelConfig.getId());
            channelConfigMapper.updateById(channelConfig);
            operationService.addOperationLog(request, "更新渠道落地页，id："+channelConfig.getId());

            //编辑落地页原本的url则需更换对应短网址
            if(!config.getUrl().equals(channelConfig.getUrl())){
                channelShortUrlService.addChannelShortUrlByCid(channelConfig.getId());
            }
        }
        return Result.returnSuccess(null);
    }

    @Override
    public Result updateStatus(HttpServletRequest request, Integer id, Integer status) throws Exception {
        ChannelConfig channelConfig = channelConfigMapper.selectById(id);
        channelConfig.setStatus(status);
        channelConfigMapper.updateById(channelConfig);

        operationService.addOperationLog(request, "更新落地页状态，id："+channelConfig.getId());

        if(status == 0){
            //添加落地页对应渠道的短网址
            channelShortUrlService.addChannelShortUrlByCid(channelConfig.getId());
        }

        return Result.returnSuccess(null);
    }
}
